/* eslint-disable */
function JCCache() {
  this.storage = window.localStorage
}
function JCCachePool(s, xzqhVersion) {
  this.sn = s
  this.xzqhVersion = xzqhVersion
  this.keys = {
    pbx: 'pbx',
    org: 'org', /* 组织机构*/
    gtype: 'gtype', /* goodstype 商品类型*/
    curstore: 'curstore', /* 当前用户组织下所有仓库*/
    //	baseprice:"baseprice",/*基础价格*/
    printurl: 'print_url',
    print_tp: 'print_tp', /* 打印模板*/
    areaprice: 'areaprice',
    corpprice: 'corpprice', /* 总公司价格*/
    serviceprice: 'serviceprice', /* 服务费*/
    crmtype: 'crmtype', /* 客户类型 */
    warnlevel: 'warnlevel', /* 安全隐患*/
    xzqh: 'xzqh', /* 行政区划*/
    call_dev_num: 'devnum' /* 呼叫中心设备编号*/
  }
  this.cache = new JCCache()
}
JCCachePool.prototype.getCacheItem = function(k) {
  return this.cache.getCacheItem(k)
}
JCCachePool.prototype.setCacheItem = function(k, c) {
  return this.cache.setCacheItem(k, c)
}
JCCachePool.prototype.removeCacheItem = function(k) {
  this.cache.removeCacheItem(k)
}
JCCachePool.prototype.getPBXCache = function() {
  return this.cache.getCacheItem(this.keys.pbx)
}
JCCachePool.prototype.setPBXCache = function(c) {
  return this.cache.setCacheItem(this.keys.pbx, c)
}
JCCachePool.prototype.getDevNumCache = function() {
  return this.cache.getCacheItem(this.keys.call_dev_num)
}
JCCachePool.prototype.setDevNumCache = function(c) {
  return this.cache.setCacheItem(this.keys.call_dev_num, c)
}
JCCachePool.prototype.setPrintUrlCache = function(c) {
  return this.cache.setCacheItem(this.keys.printurl, c)
}
JCCachePool.prototype.getPrintUrlCache = function() {
  return this.cache.getCacheItem(this.keys.printurl)
}

JCCachePool.prototype.setPrintTPCache = function(c) {
  return this.cache.setCacheItem(this.keys.print_tp, c)
}
JCCachePool.prototype.getPrintTPCache = function() {
  return this.cache.getCacheItem(this.keys.print_tp)
}

JCCachePool.prototype.getOrgCache = function(fn, dre, t, all) {
  /* 组织机构 如果能从 cache里面,这直接用cache里面的值,不再请求*/
  var tempfn = function(d) {
    if (all) {
      fn(d)
    } else if (t) {
      var tempdatas = []
      for (var i = 0; i < d.length; i++) {
        var item = d[i]
        if (item.orgType == t) {
          if (dre) {
            tempdatas.push(item)
            continue
          }
          tempdatas.push({ id: item.orgId, text: item.orgName, tag: item })
        }
      }
      fn(tempdatas)
    } else {
      var tempdatas = $dlh.tools.un.treeData(d, 'orgId', 'orgName', 'id', 'parentId', dre)

      fn(tempdatas)
    }
  }

  var objJson = this.cache.getCacheItem(this.keys.org)
  if (objJson) {
    var tempObj = $dlh.tools.json.toObj(objJson)
    if (tempObj && tempObj.data && tempObj.data.length > 0 &&
            tempObj.sn && tempObj.sn == this.sn) {
      tempfn(tempObj.data); return
    }
  }

  var _this = this
  $dlh.cmct.post({ url: 'page/sys/org/query' },
    function(rlt) {
      if (rlt.exc != null) { return }
      //
      var datas = rlt.data
      _this.cache.setCacheItem(_this.keys.org, $dlh.tools.json.toJson({ sn: _this.sn, data: datas }))

      tempfn(datas)
    })
}

export default JCCachePool
